<template>
  <div class="form-select">
    <template v-if="valueIsObject">
      <el-select v-if="formType === 'product_brand'" v-model="dataValue.select" :disabled="disabled"
        :clearable="clearable" :placeholder="placeholder" style="width: 100%;" @change="valueChange">
        <el-option v-for="(item, index) in options" :key="index" :label="item.name" :value="item.id" />
      </el-select>
      <TreeSelect v-if="formType == 'product_category'" :data="options" :props="propsTree" v-model="dataValue.select"
        @input="treeInput" />

    </template>
  </div>
</template>

<script>
import { isObject, isEmpty, isArray } from '@/utils/types'
import Emitter from 'element-ui/lib/mixins/emitter'
import { queryBrandPageList } from "@/api/product/productBrand";
import { queryMaterialPageList } from "@/api/product/productMaterial";
import TreeSelect from "../SelectTree/index.vue";



export default {
  // 自定义字段库 单选
  name: 'FormSelect',

  components: { TreeSelect },

  mixins: [Emitter],

  props: {
    value: [String, Number],
    // 选择其他展示input输入框
    otherShowInput: {
      type: Boolean,
      default: false
    },
    disabled: Boolean,
    clearable: Boolean,
    placeholder: String,
    formType: String,
    showType: {
      type: String,
      default: 'default' //  下拉 default 平铺 tiled
    }
  },

  data () {
    return {
      dataValue: {
        select: '',
        otherValue: '',
      },
      options: [],
      propsTree: {
        label: 'name',
        children: 'children'
      }
    }
  },

  computed: {
    valueIsObject () {
      return isObject(this.dataValue)
    }
  },

  watch: {
    value: {
      handler (newVal, oldVal) {
        console.log("www=====233333333333======", newVal)
        console.log("www=====4444444444======", this.value)
        this.validValue()
      },
      immediate: true
    },
  },

  created () {
    this.getData();
    // this.validValue()
  },

  mounted () {

  },

  beforeDestroy () { },

  methods: {
    treeInput (id) {
      this.dataValue.select = id;
      const value = this.dataValue.select === '其他' ? this.dataValue.otherValue.trim() : this.dataValue.select
      this.$emit('input', value)
      this.$emit('change', value)
      this.dispatch('ElFormItem', 'el.form.change', value)
    },
    getData () {
      let request = null;
      var params = {

      }
      if (this.formType == 'product_brand') {
        params.pageType = 0;
        request = queryBrandPageList;
      } else {
        request = queryMaterialPageList;
      }
      request(params).then(res => {
        if (this.formType == 'product_brand') {
          this.options = res.data.records;
          this.validValue()
        } else {
          this.options = res.data;
          this.validValue()
        }

        // this.options = res.data;

      })
    },
    /**
     * 验证值
     */
    validValue () {
      if (!isArray(this.options) || this.options.length === 0) {
        return
      }
      if (this.value !== this.dataValue.select && this.value !== this.dataValue.otherValue) {
        if (isEmpty(this.value)) {
          this.dataValue = {
            select: '',
            otherValue: ''
          }
        } else {
          this.dataValue = {
            select: this.value,
            otherValue: ''
          }
          return
          //todo-----不知道为什么otherShowInput一直显示true,正常应该显示false
          if (this.otherShowInput) {
            const valueObj = this.options.find(item => this.getValue(item) === this.value)
            if (valueObj) {
              if (this.dataValue.select !== this.value) {
                this.dataValue = {
                  select: this.value,
                  otherValue: ''
                }
              }
            } else {
              if (this.dataValue.otherValue !== this.value) {
                this.dataValue = {
                  select: '其他',
                  otherValue: this.value
                }
              }
            }
          } else {
            this.dataValue = {
              select: this.value,
              otherValue: ''
            }
          }
        }
      } else {
        if (isEmpty(this.value)) {
          if (this.dataValue.select !== '其他') {
            this.dataValue = {
              select: '',
              otherValue: ''
            }
          } else {
            this.dataValue = {
              select: '其他',
              otherValue: ''
            }
          }
        }
      }
    },

    /**
     * 选项值
     */
    getValue (item) {
      return !this.isEmptyValue(item.value) ? item.value : item
    },

    /**
     * 判断是空值
     */
    isEmptyValue (value) {
      return value === null || value == undefined
    },

    /**
     * 值更新
     */
    valueChange () {
      const value = this.dataValue.select === '其他' ? this.dataValue.otherValue.trim() : this.dataValue.select
      this.$emit('input', value)
      this.$emit('change', value)
      this.dispatch('ElFormItem', 'el.form.change', value)
    },

    /**
     * 失去焦点
     */
    inputBlur () {
      // 暂未加input触发change逻辑
      const value = this.dataValue.otherValue
      const eIsObject = this.options.length > 0 && !this.isEmptyValue(this.options[0].value)
      const has = this.options.find(item => {
        if (eIsObject) {
          return item.value === value.trim()
        } else {
          return item === value.trim()
        }
      })
      if (has) {
        this.dataValue.otherValue = ''
      }

      this.$emit('input', this.dataValue.otherValue)
      this.$emit('change', this.dataValue.otherValue)
      this.dispatch('ElFormItem', 'el.form.change', this.dataValue.otherValue)
    }
  }
}
</script>

<style lang="scss" scoped>
.form-select {
  .el-input {
    margin-top: 5px;
  }

  .el-radio {
    margin: 5px 30px 5px 0;
  }
}
</style>
